package com.beehoo.dcportal.admin.service.impl;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.beehoo.dcportal.admin.dao.OrganizationDao;
import com.beehoo.dcportal.admin.service.OrganizationService;
import com.beehoo.dcportal.common.Constant;
import com.beehoo.dcportal.common.WriteFile;
import com.beehoo.dcportal.common.base.BaseException;
import com.beehoo.dcportal.common.base.BaseService;
import com.beehoo.dcportal.model.FileBean;
import com.beehoo.dcportal.model.OrganizationBean;
import com.beehoo.dcportal.utils.CommonUtils;

public class OrganizationServiceImpl extends BaseService implements
		OrganizationService {
	
	private OrganizationDao organizationDao;

	public void setOrganizationDao(OrganizationDao organizationDao) {
		this.organizationDao = organizationDao;
	}

	@Override
	public void addOrg(OrganizationBean organizationBean) throws BaseException {
		validateOrgBean(organizationBean);
		organizationBean = disposeUploadFile(organizationBean);
		String id = organizationDao.addOrg(organizationBean);
		if(CommonUtils.nvlToString(id).equals("")){
			throw new BaseException("custom.messages.error.org.create.error");
		}

	}

	@Override
	public void delOrg(String orgId) throws BaseException {
		if(CommonUtils.nvlToString(orgId).equals("")){
			throw new BaseException("custom.messages.error.org.id.empty");
		}
		int ret = organizationDao.delOrg(orgId);
		if(ret <= 0){
			throw new BaseException("custom.messages.error.org.del.error");
		}
	}

	@Override
	public OrganizationBean queryOrg(String streetId, String orgId)
			throws BaseException {
		if(CommonUtils.nvlToString(streetId).equals("")){
			throw new BaseException("custom.messages.error.street.id.empty");
		}
		if(CommonUtils.nvlToString(orgId).equals("")){
			throw new BaseException("custom.messages.error.org.id.empty");
		}
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("streetId", streetId);
		paramMap.put("orgId", orgId);
		return organizationDao.queryOrg(paramMap);
	}

	@Override
	public List<OrganizationBean> queryOrgs(String streetId)
			throws BaseException {
		if(CommonUtils.nvlToString(streetId).equals("")){
			throw new BaseException("custom.messages.error.street.id.empty");
		}
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("streetId", streetId);
		return organizationDao.queryOrgs(paramMap);
	}

	@Override
	public void updateOrg(OrganizationBean organizationBean)
			throws BaseException {
		validateOrgBean(organizationBean);
		organizationBean = disposeUploadFile(organizationBean);
		int ret = organizationDao.updateOrg(organizationBean);
		if(ret <= 0){
			throw new BaseException("custom.messages.error.org.update.error");
		}
	}

	private void validateOrgBean(OrganizationBean organizationBean)
			throws BaseException {
		if (organizationBean == null) {
			throw new BaseException("custom.messages.error.org.create.error");
		}
		if (CommonUtils.nvlToString(organizationBean.getStreetId()).equals("")) {
			throw new BaseException("custom.messages.error.street.id.empty");
		}
		if (CommonUtils.nvlToString(organizationBean.getOrgName()).equals("")) {
			throw new BaseException("custom.messages.error.org.name.empty");
		}
	}
	
	private OrganizationBean disposeUploadFile(OrganizationBean organizationBean)
			throws BaseException {
		FileBean fileBean = organizationBean.getFileBean();
		if(fileBean != null){
			int fileSize = fileBean.getFileSize();
			if(fileSize > Constant.UPLOAD_FILE_MAX_SIZE){
				throw new BaseException("struts.messages.error.file.too.large");
			}
			if(!CommonUtils.validateImageFileType(fileBean.getContentType())){
				throw new BaseException("struts.messages.error.content.type.not.allowed");
			}
			String fileName = CommonUtils.getUploadFileName(organizationBean.getStreetId(), Constant.IMG_ORG_PHOTO);
			organizationBean.setOrgPhoto(organizationBean.getPhotoDir() + fileName);
			
			File target = new File(organizationBean.getPhotoPath() + fileName);
			WriteFile wf = new WriteFile(fileBean.getFile(), target, fileSize);
			try {
				wf.write();
			} catch (BaseException e) {
				throw new BaseException("custom.messages.error.org.upload.error");
			}
		}
		return organizationBean;
	}
}
